﻿using System;
using System.IO;
using System.Windows.Forms;
using NExplorer.AddIns.WinRar.Properties;

namespace NExplorer.AddIns.WinRar
{
	internal static class WinRarUtility
	{
		public static bool ConfirmWinRarAvailable()
		{
			bool available;

			if (File.Exists(Settings.Default.WinRarFullPath))
			{
				available = true;
			}
			else
			{
				string programFilesWinRar = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), "WinRar");
				string winRarFullPath = Path.Combine(programFilesWinRar, "winrar.exe");

				if (File.Exists(winRarFullPath))
				{
					Settings.Default.WinRarFullPath = winRarFullPath;
					available = true;
				}
				else
				{
					MessageBox.Show("The location of WinRar.exe can't be determined. Please locate it on the next dialog.", "Can't find WinRar.exe", MessageBoxButtons.OK, MessageBoxIcon.Information);

					OpenFileDialog dialog = new OpenFileDialog();
					dialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
					dialog.CheckFileExists = true;

					if (dialog.ShowDialog() == DialogResult.OK)
					{
						Settings.Default.WinRarFullPath = dialog.FileName;
						available = true;
					}
					else
					{
						available = false;						
					}
				}

				if (available)
				{
					Settings.Default.Save();
				}
			}

			return available;
		}
	}
}
